<?php


namespace app\common\lib;

class Page2
{
    private $total;      //总记录
    private $pageSize;   //每页显示多少条
    private $limit;      //limit
    private $curPage;       //当前页码
    private $totalPages; //总页码
    private $url;        //地址
    private $bothNum;    //两边保持数字分页的量

    public function __construct($_total, $_pageSize)
    {
        $this->total = $_total ? $_total : 1;
        $this->pageSize = $_pageSize;
        $this->totalPages = ceil($this->total / $this->pageSize);
        $this->curPage = $this->setPage();
        $this->limit = "LIMIT " . ($this->curPage - 1) * $this->pageSize . ", $this->pageSize";
        $this->url = $this->getUrl();
        $this->bothNum = 2;
    }

    // 获取当前页码
    private function setPage()
    {
        $page = intval($_GET['page']);
        if ($page && $page > 0) {
            if ($page > $this->totalPages) {
                return $this->totalPages;
            }
            return $page;
        }
        return 1;
    }

    //获取地址
    private function getUrl()
    {
        $_url = $_SERVER["REQUEST_URI"];
        $_par = parse_url($_url);
        if (isset($_par['query'])) {
            parse_str($_par['query'], $_query);
            unset($_query['page']);
            $_url = $_par['path'] . '?' . http_build_query($_query);
        }
        return $_url;
    }

    // 页码
    private function pageList()
    {
        $_pageList = '';
        for ($i = $this->bothNum; $i >= 1; $i--) {
            $_page = $this->curPage - $i;
            if ($_page < 1) continue;
            $_pageList .= ' <a href="' . $this->url . '&page=' . $_page . '">' . $_page . '</a> ';
        }
        $_pageList .= ' <span class="me">' . $this->curPage . '</span> ';
        for ($i = 1; $i <= $this->bothNum; $i++) {
            $_page = $this->curPage + $i;
            if ($_page > $this->totalPages) break;
            $_pageList .= ' <a href="' . $this->url . '&page=' . $_page . '">' . $_page . '</a> ';
        }
        return $_pageList;
    }

    //首页
    private function first()
    {
        if ($this->curPage > $this->bothNum + 1) {
            return ' <a href="' . $this->url . '">1</a> ...';
        }
    }

    //上一页
    private function prev()
    {
        if ($this->curPage == 1) {
            return '<span class="disabled">上一页</span>';
        }
        return ' <a href="' . $this->url . '&page=' . ($this->curPage - 1) . '">上一页</a> ';
    }

    //下一页
    private function next()
    {
        if ($this->curPage == $this->totalPages) {
            return '<span class="disabled">下一页</span>';
        }
        return ' <a href="' . $this->url . '&page=' . ($this->curPage + 1) . '">下一页</a> ';
    }

    //尾页
    private function last()
    {
        if ($this->totalPages - $this->curPage > $this->bothNum) {
            return ' ...<a href="' . $this->url . '&page=' . $this->totalPages . '">' . $this->totalPages . '</a> ';
        }
    }

    //分页信息
    public function showPages()
    {
        $_page = '';
        $_page .= $this->first();
        $_page .= $this->pageList();
        $_page .= $this->last();
        $_page .= $this->prev();
        $_page .= $this->next();
        return $_page;
    }
}